import {
	ref
} from "vue";

export default function useGetComment(data, $http) {
	/*获取文章评论列表*/
	const {
		articleId,
		pageSize,
		page
	} = data;
	
	const oldList = ref([]);
	const _getCommentList = async () => {
		try {
			// 从ref对象中获取最新值并确保类型正确
			const currentArticleId = articleId?.value || articleId || '';
			const currentPageSize = Number(pageSize?.value || pageSize || 5);
			const currentPage = Number(page?.value || page || 1);
			
			// 确保articleId有效且page为正整数
			if (!currentArticleId || currentPage < 1 || !Number.isInteger(currentPage)) {
				console.warn('参数无效，跳过评论请求');
				return [];
			}
			
			const listArr = await $http.get_comments({
				articleId: currentArticleId,
				pageSize: currentPageSize,
				page: currentPage,
			});
			
			// 检查返回的数据是否为空数组或者重复数据
			if (!listArr || listArr.length === 0) {
				return [];
			}
			
			// 检查是否有重复数据（简单去重逻辑）
			const newUniqueComments = listArr.filter(newComment => 
				!oldList.value.some(existingComment => existingComment.comment_id === newComment.comment_id)
			);
							
			oldList.value.push(...newUniqueComments);
			return newUniqueComments;
		} catch (error) {
			console.error("获取评论列表失败", error);
			return [];
		}
	};

	return {
		oldList,
		_getCommentList,
	};
}